package com.egame.manage.biz.server.cu;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.ext.cu.ManufactureInfo;
import com.egame.manage.biz.server.ManageClientExt;
import com.egame.manage.biz.domain.ManufactureBean;

public class ManufacturerServiceDao extends BaseDao {

	public ManufacturerServiceDao() throws ExceptionCommonBase {
		super("egame_ext");
	}
	
	public int setManufactureInfo(ManufactureBean item)throws ExceptionCommonBase{
		String sql = "insert into t_customer_manufacturer(cn_name, en_name, en_short, short_name,remark, operator_id,insert_time,update_time) VALUES ("; 
		sql +=  SqlUtils.QuataStr(item.getCnName()) +",";
		sql +=  SqlUtils.QuataStr(item.getEnName()) +",";
		sql += SqlUtils.QuataStr(item.getEnShort()) +",";
		sql += SqlUtils.QuataStr(item.getShortName()) +",";
		sql	+= SqlUtils.QuataStr(item.getRemark()) +",";		
		sql += item.getOperator() + ",";
		sql += "now(),now())";
		System.out.println(sql);
		return this.executeUpdate(sql);
	
	}
	
	public int deleteManufacturer(int manufacturerId)throws ExceptionCommonBase{
		String sql = "delete from t_customer_manufacturer where manufacturer_id = " + manufacturerId;
		return this.executeUpdate(sql);
	}
	
	public int updateManufactureInfo(ManufactureInfo bean)throws ExceptionCommonBase{
		if(bean !=null){
			String sql = "UPDATE t_customer_manufacturer SET" +
					" cn_name = " + SqlUtils.QuataStr(bean.getCnName()) + ", " +
					" en_name = " + SqlUtils.QuataStr(bean.getEnName()) + ", " +
					" en_short = " + SqlUtils.QuataStr(bean.getEnShort()) + ", " +
					" short_name = " + SqlUtils.QuataStr(bean.getShortName()) + ", " +
					" remark = " + SqlUtils.QuataStr(bean.getRemark()) + ", " +
					" update_time= now() " +
					" WHERE manufacturer_id = " + bean.getManufactureId();
			return this.executeUpdate(sql);
		}
		return -1;
	}
	
	public Map<String, Object> listManufacturer()throws ExceptionCommonBase{
		String sql = "select manufacturer_id,short_name from t_customer_manufacturer";
		List<Map<String, Object>> maps = this.getListKeyValue(sql);
		Map<String, Object> result = new HashMap<String, Object>();
		for(Map<String, Object> map:maps){
			result.putAll(map);
		}
		return result;
	}
	
	public List<String[]> listManufacturerBand()throws ExceptionCommonBase{
		String sql = "select manufacturer_id,en_short,short_name from t_customer_manufacturer";
		return this.getListStrings(sql);		
	}
	
	public String getManufactureName(int appId,long loginUserId,int mId)throws RemoteException{
		ManufactureInfo bean = ManageClientExt.getInstance().getManufactureInfoById(appId, loginUserId, mId);
		return bean==null?"":bean.getCnName();
	}

	public List<Integer> listManufacturer(ManufactureInfo bean, int page,int rowsOfPage) throws ExceptionCommonBase {
		String sql = "select manufacturer_id from t_customer_manufacturer where 1= 1";
		if(bean != null){
			if(!Utils.stringIsNullOrEmpty(bean.getShortName())){
				sql += " and short_name like " + SqlUtils.QuataLikeStr(bean.getShortName());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getCnName())){
				sql += " and cn_name like " + SqlUtils.QuataLikeStr(bean.getCnName());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getEnName())){
				sql += " and en_name like " + SqlUtils.QuataLikeStr(bean.getEnName());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getEnShort())){
				sql += " and en_short like " + SqlUtils.QuataLikeStr(bean.getEnShort());
			}
		}
		 sql += " limit " + page * rowsOfPage + ","	+ rowsOfPage;
		 System.out.println(sql);
		 return this.getListInteger(sql);
	}

	public int getCount(ManufactureInfo bean) throws ExceptionCommonBase {
		String sql = "select count(manufacturer_id) from t_customer_manufacturer where 1= 1";
		if(bean != null){
			if(!Utils.stringIsNullOrEmpty(bean.getShortName())){
				sql += " and short_name like " + SqlUtils.QuataLikeStr(bean.getShortName());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getCnName())){
				sql += " and cn_name like " + SqlUtils.QuataLikeStr(bean.getCnName());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getEnName())){
				sql += " and en_name like " + SqlUtils.QuataLikeStr(bean.getEnName());
			}
			if(!Utils.stringIsNullOrEmpty(bean.getEnShort())){
				sql += " and en_short like " + SqlUtils.QuataLikeStr(bean.getEnShort());
			}
		}
		return this.getInt(sql);
	}
	
}
